package com.ruoyi.lock.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lock.domain.ACheckaIn;
import com.ruoyi.lock.service.IACheckaInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 打卡Controller
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/lock/in")
public class ACheckaInController extends BaseController
{
    @Autowired
    private IACheckaInService aCheckaInService;

    /**
     * 查询打卡列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:in:list')")
    @GetMapping("/list")
    public TableDataInfo list(ACheckaIn aCheckaIn)
    {
        startPage();
        List<ACheckaIn> list = aCheckaInService.selectACheckaInList(aCheckaIn);
        return getDataTable(list);
    }

    /**
     * 导出打卡列表
     */
    @PreAuthorize("@ss.hasPermi('lock:in:export')")
    @Log(title = "打卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ACheckaIn aCheckaIn)
    {
        List<ACheckaIn> list = aCheckaInService.selectACheckaInList(aCheckaIn);
        ExcelUtil<ACheckaIn> util = new ExcelUtil<ACheckaIn>(ACheckaIn.class);
        util.exportExcel(response, list, "打卡数据");
    }

    /**
     * 获取打卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('lock:in:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aCheckaInService.selectACheckaInById(id));
    }

    /**
     * 新增打卡
     */
//    @PreAuthorize("@ss.hasPermi('lock:in:add')")
    @Log(title = "打卡", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody ACheckaIn aCheckaIn)
    {
        return toAjax(aCheckaInService.insertACheckaIn(aCheckaIn));
    }

    /**
     * 修改打卡
     */
    @PreAuthorize("@ss.hasPermi('lock:in:edit')")
    @Log(title = "打卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ACheckaIn aCheckaIn)
    {
        return toAjax(aCheckaInService.updateACheckaIn(aCheckaIn));
    }

    /**
     * 删除打卡
     */
    @PreAuthorize("@ss.hasPermi('lock:in:remove')")
    @Log(title = "打卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aCheckaInService.deleteACheckaInByIds(ids));
    }


    /**
     * 查询需要打卡的锁的经纬度
     */
//    @PreAuthorize("@ss.hasPermi('lock:in:list')")
    @GetMapping("/selectCheckaInDirection")
    public TableDataInfo selectCheckaInDirection(@RequestParam("phone") String phone)
    {
//        startPage();
        List<ACheckaIn> list = aCheckaInService.selectCheckaInDirection(phone);
        return getDataTable(list);
    }

    /**
     * 查询打卡记录
     */
//    @PreAuthorize("@ss.hasPermi('lock:in:list')")
    @GetMapping("/checkAInList")
    public TableDataInfo checkAInList(ACheckaIn aCheckaIn)
    {
        PageHelper.startPage(aCheckaIn.getPageNum(),aCheckaIn.getPageSize()).setReasonable(false);
//        startPage();
        List<ACheckaIn> list = aCheckaInService.checkAInList(aCheckaIn);
        return getDataTable(list);
    }

    /**
     * 查近十天内有打卡记录的数据
     */
    @GetMapping("/checkAInListByDate")
    public TableDataInfo checkAInListByDate(ACheckaIn aCheckaIn)
    {
        List<JSONObject> list = aCheckaInService.checkAInListByDate(aCheckaIn);
        return getDataTable(list);
    }
}
